
// const LunarCalendar = require("lunar-calendar");

// function getLunarDate(date) {
//   const lunarDate = LunarCalendar.solarToLunar(date.getFullYear(), date.getMonth() + 1, date.getDate());
//   return {
//     year: lunarDate.lYear,
//     month: lunarDate.lMonth,
//     day: lunarDate.lDay,
//   };
// }

// 示例用法
// const date = new Date("2023-08-22");
// const lunarDate = getLunarDate(date);
// console.log("农历时间：", lunarDate.year, lunarDate.month, lunarDate.day);


//  判断工作日

function isWeekend(date) {
  return date.getDay() === 0 || date.getDay() === 6; // 周六和周日是周末
}

// 2023年节假日信息 
function isHoliday(date) {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  // 元旦
  if (month === 1 && day >= 1 && day <= 2) {
    return true;
  }

  //   除夕21 春节22
  if (month === 1 && day >= 21 && day <= 27) {
    return true;
  }

  // 清明节
  if (month === 4 && day > 4 && day < 6) {
    return true;
  }

  // 劳动节
  if (month === 5 && day >= 1 && day <= 3) {
    return true;
  }

  // 端午节
  if (month === 6 && day >= 22 && day <= 24) {
    return true;
  }

  // 中秋节
  if (month === 9 && day >= 29 && day <= 30) {
    return true;
  }

  // 国庆节
  if (month === 10 && day >= 1 && day < 7) {
    return true;
  }

  return false;
}


function countWorkdays(startDate, endDate) {
  let currentDate = new Date(startDate);
  let workdays = 0;

  while (currentDate <= endDate) {
    if (!isWeekend(currentDate) && !isHoliday(currentDate)) {
      workdays++;
    }

    currentDate.setDate(currentDate.getDate() + 1);
  }

  return workdays;
}


// 监听事件，等待html页面加载完，再执行
document.addEventListener("DOMContentLoaded", function () {
  document.getElementById("calculateButton").addEventListener("click", function () {
    const startDate = new Date(document.getElementById("startDate").value);
    const endDate = new Date(document.getElementById("endDate").value);
    const workdays = countWorkdays(startDate, endDate);
    document.getElementById("result").textContent = "工作日总天数：" + workdays;

    let holidayDates = [];
    let weekendDates = [];

    let currentDate = new Date(startDate);
    while (currentDate <= endDate) {
      if (isHoliday(currentDate)) {
        holidayDates.push(currentDate.toLocaleDateString());
      } else if (isWeekend(currentDate)) {
        weekendDates.push(currentDate.toLocaleDateString());
      }

      currentDate.setDate(currentDate.getDate() + 1);
    }
    // console.log("weekendDates=",weekendDates);

    //  年份去除
    // function formatMonthAndDay(dateString) {
    //   let newDate = []

    //   if(dateString.length <= 0) return newDate
    //   for(let i = 0 ;i<dateString;i++){

    //     const parts = dateString[i].split("-");
    //     newDate = parts[1] + "-" + parts[2];
    //     console.log("newDate=",newDate); // 输出 "8-23"

    //   }
    //   // return parts[1] + "-" + parts[2];
    //   return newDate
    // }
    
    // 测试
    // const originalDate = "2023-8-23";
    // 节假日
    // let holiDates = formatMonthAndDay(holidayDates);
    // //工作日
    // // weekendDates = formatMonthAndDay(weekendDates);
    // let weekDates = formatMonthAndDay(weekendDates);

    

    document.getElementById("holidayResult").textContent = "节假日时间：" + holidayDates.join(", ") + "。总计：" + holidayDates.length + "天。";
    document.getElementById("weekendResult").textContent = "周末时间：" + weekendDates.join(", ") + "。总计：" + weekendDates.length + "天。";

    // document.getElementById("holidayResult").textContent = "节假日时间：" + holiDates + "。总计：" + holidayDates.length + "天。";
    // document.getElementById("weekendResult").textContent = "周末时间：" + weekDates + "。总计：" + weekendDates.length + "天。";

  });
});



// 示例用法
// const startDate = new Date("2023-08-01");
// const endDate = new Date("2023-08-15");
// const workdays = countWorkdays(startDate, endDate);
// console.log("工作日天数:", workdays);


// // 定义一些日期
// const date1 = '2023-06-01';
// const date2 = '2023-06-30';
// // 创建一个moment对象，然后使用isBetween函数来检查一个日期是否在两个日期之间
// let startDate = moment(date1, 'YYYY-MM-DD');
// let endDate = moment(date2, 'YYYY-MM-DD');
// let currentDate = moment(startDate, 'YYYY-MM-DD');
// let businessDays = 0;
// while (currentDate <= endDate) {
// if (!currentDate.isWeekend() && !currentDate.isHoliday()) {
// businessDays++;
// }
// currentDate.add(1, 'days');
// }
// console.log(businessDays); // 输出：21

